import { fetchToken, fetch, fetchRefreshToken } from '@/utils/fetch'
const auth_url = '/auth-center'
const sys_url = '/sysinfo-service'
export function login(username, password, identificationCode) {
  return fetch({
    url: `${auth_url}/authorize`,
    method: 'post',
    params: {
      valid_time: 'ONE_DAY',
      grant_type: 'password',
      username,
      password,
      identificationCode
    },
  	transformRequest:[function () {
  	  return JSON.stringify({username, password, identificationCode})
  	}]
  }, true, true, false, true)
}

export function getInfo() {
  return fetchToken({
    url: `${sys_url}/user`,
    method: 'get'
  })
}

export function logout() {
  return fetchRefreshToken({
    url: `${auth_url}/logout`,
    method: 'post'
  }, false, '退出成功')
}

/*token过期后申请新的token*/
export function getNewToken(token) {
  return fetch({
    url: `${auth_url}/authorize`,
    method: 'post',
    params: {
      // valid_time: 'ONE_DAY',
      grant_type: 'refresh_token',
      refresh_token: token
    },
    transformRequest:[function () {
      return JSON.stringify({token})
    }]
  }, true, true)
}

export function getIdentifyImg() { // 获取图片验证码信息
  return fetchToken({
    url: `${auth_url}/identifying/identifying_data`,
    method: 'get'
  })
}
export function verifyCode(data) { // 验证
  return fetchToken({
	url: `${auth_url}/identifying/check`,
	method: 'post',
  data
  })
}